<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
$(function () {
	//定义搜索区域字段
	var searchItems = [
		<#list table.columns as column>
		<#if !column.pk && column.columnNameLower != 'createBy'
		&& column.columnNameLower != 'createDate' && column.columnNameLower != 'updateBy'
		&& column.columnNameLower != 'updateDate' && column.columnNameLower != 'password'
		&& column.columnNameLower != 'version'>
		{
			id: "textSearch${column.columnName}",
			field: "${column.columnNameLower}",
			title: "${column.columnAlias!}"
		}<#if column_has_next>,</#if>
		</#if>
		</#list>
	];

	//初始化搜索区域
	JEE.initSearch({
        searchItems: searchItems,
        pasteTable: "tb_data" //关联表格id
	});

	//初始化表格
	JEE.initTable({
        button:{
            id:"tb_btn",
            items:[{
                id: "btn_add",
                icon: "layui-icon layui-icon-add-1",
                title: "新增",
                onClick: function () {
                    showDialog();
                }
            },
                {
                    id: "btn_edit",
                    icon: "layui-icon layui-icon-edit",
                    css: "layui-btn-normal",
                    title: "编辑",
                    onClick: function () {
                        var checkStatus = layui.table.checkStatus('tb_data');
                        if(checkStatus.data.length == 0 || checkStatus.data.length > 1){
                            JEE.error("请选择需要修改的数据");
                            return;
                        }
                        var data = checkStatus.data[0];
                        showDialog(data);
                    }
                },
                {
                    id: "btn_delete",
                    icon: "layui-icon layui-icon-delete",
                    css: "layui-btn-danger",
                    title: "删除",
                    onClick: function () {
                        var checkStatus = layui.table.checkStatus('tb_data');
                        if(checkStatus.data.length == 0 ){
                            JEE.error("请选择需要删除的数据");
                            return;
                        }
                        var ids = [];
                        $.each(checkStatus.data, function (i, item) {
                            ids.push(item.id);
                        });

                        JEE.confirm("是否确认删除数据？", function(){
                            $.when(JEE.ajax("/${namespace}/${classNameLower}/deleteBatch", {ids: ids})).done(function(res){
                                if(res){
                                    layui.layer.closeAll('dialog');
                                    layui.table.reload('tb_data');
                                }
                            });
                        });
                    }
                }
            ]
        },
		id: "tb_data",
		mainSearch: searchItems,
		url: "/${namespace}/${classNameLower}/listByPage",
		columns: [{
                type: "checkbox",
                fixed: "left"
			},
			<#list table.columns as column>
			<#if !column.pk && column.columnNameLower != 'createBy'
			&& column.columnNameLower != 'createDate' && column.columnNameLower != 'updateBy'
			&& column.columnNameLower != 'updateDate' && column.columnNameLower != 'password'
			&& column.columnNameLower != 'version'>
			{
				field: "${column.columnNameLower}",
				title: "${column.columnAlias!}"
			}<#if column_has_next>,</#if>
			</#if>
			</#list>
		]
	});
});

function showDialog(data){
	var flag = !data ? false : true;
	
    index = JEE.initDialog({
        title: !data ? "新增":"编辑",
        data: data,
        url: "${className}Form.html",
		disable:{}
    });
}
